﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Linq;
using UnityEngine.UI;
using System.Text;

public class Pan : MonoBehaviour
{
    private int numLetters;
    private string word, panWord;
    private GameLevel gameLevel;
    private List<Vector3> letterPositions = new List<Vector3>();
    private List<Vector3> letterLocalPositions = new List<Vector3>();
    private List<GameObject> letterTexts = new List<GameObject>();
    private List<int> indexes = new List<int>();

    private int level;
    private PanSetting panSetting;

    public Transform centerPoint;
    public RectTransform imageTr;
    public TextPreview textPreview;
    public List<PanSetting> panSettings;

    public static Pan instance;

    private void Awake()
    {
        instance = this;
    }

    private void Start()
    {
        level = Prefs.CurrentLevel;
    }

    public void Load(GameLevel gameLevel)
    {
        this.gameLevel = gameLevel;
        numLetters = gameLevel.word.Trim().Length;
        panSetting = panSettings[Mathf.Min(numLetters - 1, panSettings.Count - 1)];

        imageTr.sizeDelta = imageTr.sizeDelta * panSetting.scale;

        float delta = 360f / numLetters;

        float angle = 90;
        for (int i = 0; i < numLetters; i++)
        {
            float angleRadian = angle * Mathf.PI / 180f;
            float x = Mathf.Cos(angleRadian);
            float y = Mathf.Sin(angleRadian);
            Vector3 position = panSetting.letterRadius * new Vector3(x, y, 0);

            letterLocalPositions.Add(position);
            letterPositions.Add(centerPoint.TransformPoint(position));

            angle += delta;
        }

        LineDrawer.instance.letterPositions = letterPositions;

        for(int i = 0; i < numLetters; i++)
        {
            GameObject letter = Instantiate(MonoUtils.instance.letter);
            letter.transform.SetParent(centerPoint);
            letter.transform.localScale = Vector3.one * panSetting.letterScale;
            letter.transform.localRotation = Quaternion.Euler(new Vector3(0, 0, Random.Range(-7, 7)));

            var letterText = letter.GetComponentInChildren<Text>();
            letterText.text = gameLevel.word[i].ToString().ToUpper();
            letterTexts.Add(letter);
        }

        indexes = Prefs.GetPanWordIndexes(level).ToList();
        if (indexes.Count != numLetters)
        {
            indexes = Enumerable.Range(0, numLetters).ToList();
            indexes.Shuffle(level);
            Prefs.SetPanWordIndexes(level, indexes.ToArray());
        }

        GetPanWord();

        Timer.Schedule(this, 0, () =>
        {
            for (int i = 0; i < numLetters; i++)
            {
                letterTexts[i].transform.localPosition = letterLocalPositions[indexes.IndexOf(i)];
            }
        });
    }

    private void GetShuffeWord()
    {
        List<int> origin = new List<int>();
        origin.AddRange(indexes);
        while (true)
        {
            indexes.Shuffle();
            if (!origin.SequenceEqual(indexes)) break;
        }
        GetPanWord();
    }

    private void GetPanWord()
    {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < numLetters; i++)
        {
            sb.Append(gameLevel.word[indexes[i]]);
        }
        panWord = sb.ToString();
        textPreview.word = panWord.ToUpper();
    }

    public void Shuffle()
    {
        GetShuffeWord();
        Prefs.SetPanWordIndexes(level, indexes.ToArray());

        int i = 0;
        foreach(var text in letterTexts)
        {
            iTween.MoveTo(text.gameObject, iTween.Hash("position", letterLocalPositions[indexes.IndexOf(i)], "time", 0.15f, "isLocal", true));
            text.transform.localRotation = Quaternion.Euler(new Vector3(0, 0, Random.Range(-7, 7)));
            i++;
        }
        Sound.instance.PlayButton();
    }
}

[System.Serializable]
public class PanSetting
{
    public float scale;
    public float letterRadius;
    public float letterScale;
}
